//
//  MYGStrangMessageViewController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/5/22.
//

import UIKit

class MYGStrangMessageViewController: MYGBaseController {

    private var contents: [MYGChatListSection] = []
    var strangeMessageList: [MYGIMMessageListSessionModel]? {
        didSet {
            guard let list = strangeMessageList else {return}
            
            var chatSection = MYGChatListSection(items: [])

            for item in list  {
                var model = MYGChatListCellModel(image: item.sessionInfo.pushUser.avatar, name: item.sessionInfo.pushUser.nickname, content: item.lastMessage.last_message, time: item.lastMessage.last_time,id: 1,unreadMessagesNumber:item.unreadMessagesNumber)
                if item.lastMessage.is_destruction == 1 {
                    model.content = "阅后即焚"
                }
                model.clickAction = {[weak self] in
                    let vc = MYGChatController(sessionId: item.sessionInfo.id,mo_shenggx:1,title: item.sessionInfo.pushUser.nickname)
                    vc.unreadMessagesNumber = item.unreadMessagesNumber
                    self?.navigationController?.pushViewController(vc)
                }
                chatSection.items.append(model)
            }
            contents.append(chatSection)
            tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
        // MARK: - UI
        private func configUI() {
            self.title = "陌生消息"
            view.addSubview(tableView)
            
            tableView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
        
        // MARK: - lazy load
        private lazy var tableView: UITableView = {
            let tableV = UITableView()
            tableV.separatorStyle = .none
            tableV.delegate = self
            tableV.dataSource = self
            tableV.backgroundColor = .white
            tableV.register(cellWithClass: MYGChatListCell.self)
            return tableV
        }()
}
extension MYGStrangMessageViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents[section].items.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        70
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGChatListCell.self, for: indexPath)
        cell.model = contents[indexPath.section].items[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = .rgba(250, 250, 250, 1)
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return contents[section].footerHeight
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        contents[indexPath.section].items[indexPath.row].clickAction?()
    }
}
